package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ScoreMovieAdapter;
import flc.ast.databinding.ActivityScoreMovieBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class ScoreMovieActivity extends BaseAc<ActivityScoreMovieBinding> {

    private ScoreMovieAdapter movieAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.activity_score_movie;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivScoreMovieBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvMovieList.setLayoutManager(new LinearLayoutManager(mContext));
        movieAdapter = new ScoreMovieAdapter();
        mDataBinding.rvMovieList.setAdapter(movieAdapter);
        movieAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.SCORE_MOVIE_DATA, StkResApi.createParamMap(0, 30), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        Collections.sort(result, new Comparator<StkResBean>() {
                            @Override
                            public int compare(StkResBean o1, StkResBean o2) {
                                double score1 = (double) o1.getScore_total() / (o1.getScore_count() == 0 ? 1 : o1.getScore_count());
                                double score2 = (double) o2.getScore_total() / (o2.getScore_count() == 0 ? 1 : o2.getScore_count());
                                return Double.compare(score2, score1);
                            }
                        });
                        movieAdapter.setList(result);
                    }
                }
            }
        });
    }

}